﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmReceiptList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int Receipt_ID = 0;
        public frmReceiptList()
        {
            InitializeComponent(); btnDel.Enabled = Program.isAdmin;
            DisplayFrame();
            LoadData();
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Receipt_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", ColumnWidth.RowNumber, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Code", "Phiếu NK", "", ColumnWidth.Code3, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Date", "Ngày nhập", "C", ColumnWidth.Date, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Name", "Nhà CC", "", ColumnWidth.CustomerName, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Sender", "Người giao", "", ColumnWidth.PersonName, true, true);
            UISetting.AddColumn(ref dgvList1, "Users_FullName", "Người nhận", "", ColumnWidth.PersonName, true, true);
        }
        private void LoadData()
        {
            DataTable dt = ReceiptDB.GetList(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text), "NCC", currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["Receipt_ID"].Value = dt.Rows[i]["Receipt_ID"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Code"].Value = dt.Rows[i]["Receipt_Code"].ToString();
                dgvList1.Rows[i].Cells["Provider_Name"].Value = dt.Rows[i]["Provider_Name"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Date"].Value = Convert.ToDateTime(dt.Rows[i]["Receipt_Date"]).ToString("dd/MM/yyyy");
                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Sender"].Value = dt.Rows[i]["Receipt_Sender"].ToString();
                dgvList1.Rows[i].Cells["Users_FullName"].Value = dt.Rows[i]["Users_FullName"].ToString();
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                Receipt_ID = Convert.ToInt32(dgvList1["Receipt_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                if (ReceiptDB.Using(Receipt_ID) > 0)
                {
                    MessageBox.Show("Phiếu nhập kho này đang sử dụng! Không xóa được.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(Message.Delete_Question, Message.Info, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (ReceiptDB.Delete(Receipt_ID))
                    {
                        Program.taskbarNotifier.Show(Message.Info, Message.Delete_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(Message.Delete_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                Receipt_ID = Convert.ToInt32(dgvList1["Receipt_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                Form f = new frmReceipt(Receipt_ID);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmReceipt(0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex>=0)
            btnEdit_Click(null,null);
        }


    }
}